<template>
  <div class="dashboard-container">
    <div class="dashboard-text">
      <div class="table-head">
        <el-input
          v-model="foodSearch"
          placeholder="请输入菜品名称"
          prefix-icon="el-icon-search"
          style="width: 300px"
          @input="handleInput"
          @keyup.enter.native="getFoodList"
        />
        <div class="right">
          <el-button type="text" style="color: #f56c6c">批量删除</el-button>
          <el-divider direction="vertical" />
          <el-button type="text" @click="handelToSell(multipleSelection, 1)"
            >批量启售</el-button
          >
          <el-divider direction="vertical" />
          <el-button
            type="text"
            style="color: #f56c6c"
            @click="handelToSell(multipleSelection, 0)"
            >批量停售</el-button
          >
          <el-button type="warning" icon="el-icon-plus" @click="toAddFood"
            >新建菜品</el-button
          >
        </div>
      </div>
    </div>
    <div class="table-body">
      <div class="inner-table">
        <el-table
          ref="multipleTable"
          :data="tableData"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="name" label="菜品名称" align="center" />
          <el-table-column prop="image" label="图片" align="center">
            <template #default="{ row }">
              <el-image
                style="width: 50px; height: 50px"
                :src="`${env}/common/download?name=${row.image}`"
                :preview-src-list="[`${env}/common/download?name=${row.image}`]"
                fit="fill"
              />
            </template>
          </el-table-column>
          <el-table-column
            prop="categoryName"
            label="菜品分类"
            align="center"
          />
          <el-table-column prop="price" label="售价" align="center" />
          <el-table-column prop="status" label="售卖状态" align="center">
            <template #default="sellStatus">
              {{ sellStatus.row.status === 0 ? unsell : sell }}
            </template>
          </el-table-column>
          <el-table-column
            prop="updateTime"
            label="最后操作时间"
            align="center"
          />
          <el-table-column prop="id" label="操作" align="center">
            <template #default="scope">
              <el-button
                type="text"
                size="default"
                @click="editFootMsg(scope.row)"
              >
                修改
              </el-button>
              <el-divider direction="vertical" />
              <el-popconfirm
                title="确认调整该账号的状态?"
                @onConfirm="handelToSell([scope.row], +!scope.row.status)"
              >
                <el-button slot="reference" size="default" type="text">
                  {{ scope.row.status === 0 ? sell : unsell }}
                </el-button>
              </el-popconfirm>
              <el-divider direction="vertical" />
              <el-popconfirm
                title="确认删除该账号?"
                @onConfirm="handleChange(scope.row)"
              >
                <el-button
                  slot="reference"
                  type="text"
                  size="default"
                  style="color: red"
                >
                  删除
                </el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="table-foot">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 20, 30, 40]"
        :total="totalPage"
        :current-page="pageInfo.page"
        :page-size="pageInfo.pageSize"
        @current-change="foodPageChange"
        @size-change="foodSizeChange"
      />
    </div>
  </div>
</template>

<script>
import { getDishPage, dishStatusByStatus } from "@/api/foodManage";
export default {
  data() {
    return {
      env: process.env.VUE_APP_BASE_API,
      // 搜索内容
      foodSearch: "",
      // 表格数据
      tableData: [],
      // 售卖状态 1:启售 0:停售
      sell: "启售",
      unsell: "停售",
      // 表格选中数据
      multipleSelection: [],
      // 总页数
      totalPage: 10,
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
    };
  },
  created() {
    this.getFoodList();
    this.debounceSearch = this.$createDebounce(this.getFoodList, 500);
  },
  methods: {
    handleInput() {
      this.debounceSearch();
    },
    // 获取列表数据
    getFoodList() {
      if (this.debounceSearch && this.debounceSearch.cancel) {
        this.debounceSearch.cancel();
      }
      getDishPage({ ...this.pageInfo, name: this.foodSearch }).then((res) => {
        this.totalPage = res.data.total;
        this.tableData = res.data.records;
      });
    },
    // 选择列表数据
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log("handleSelectionChange", this.multipleSelection);
    },
    // 批量启售-------------
    handelToSell(array, status) {
      if (array.length === 0) {
        this.$message.error("未选中项目");
        return;
      }
      dishStatusByStatus(
        { ids: array.map((item) => item.id).join() },
        status
      ).then((res) => {
        this.$message.success("操作成功");
        this.getFoodList();
      });
    },
    // 批量停售
    handelAllToUnsell() {},
    // 修改菜品
    editFootMsg(msg) {
      // console.log('修改菜品', msg)
      this.$router.push({
        name: "editFood",
        query: { isEdit: "1", msg: JSON.stringify(msg) },
      });
    },
    foodPageChange(val) {
      this.pageInfo.page = val;
      this.getFoodList();
    },
    foodSizeChange(val) {
      this.pageInfo.pageSize = val;
      this.getFoodList();
    },
    // 添加菜品
    toAddFood() {
      this.$router.push({ name: "addFood" });
    },
  },
};
</script>

<style lang="scss" scoped>
.table-head {
  display: flex;
  justify-content: space-between;
}
</style>
